package com.mathlearning.auth.controller;

import com.mathlearning.auth.common.ApiResponse;
import com.mathlearning.auth.dto.comment.*;
import com.mathlearning.auth.dto.discussion.LikeResponse;
import com.mathlearning.auth.service.CommentService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 获取讨论的评论列表
     */
    @GetMapping("/discussions/{discussionId}/comments")
    public ApiResponse<CommentListResponse> getCommentList(
            @PathVariable Long discussionId,
            CommentListRequest request,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        CommentListResponse response = commentService.getCommentList(discussionId, request, userId);
        return ApiResponse.success(response);
    }

    /**
     * 发布评论
     */
    @PostMapping("/discussions/{discussionId}/comments")
    public ApiResponse<CommentResponse> createComment(
            @PathVariable Long discussionId,
            @Valid @RequestBody CreateCommentRequest request,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        CommentResponse response = commentService.createComment(discussionId, request, userId);
        return ApiResponse.success("评论成功", response);
    }

    /**
     * 获取评论的回复列表
     */
    @GetMapping("/comments/{commentId}/replies")
    public ApiResponse<CommentListResponse> getCommentReplies(
            @PathVariable Long commentId,
            CommentListRequest request,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        CommentListResponse response = commentService.getCommentReplies(commentId, request, userId);
        return ApiResponse.success(response);
    }

    /**
     * 点赞/取消点赞评论
     */
    @PostMapping("/comments/{id}/like")
    public ApiResponse<LikeResponse> likeComment(
            @PathVariable Long id,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        LikeResponse response = commentService.likeComment(id, userId);
        return ApiResponse.success("操作成功", response);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/comments/{id}")
    public ApiResponse<?> deleteComment(
            @PathVariable Long id,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        commentService.deleteComment(id, userId);
        return ApiResponse.success("删除成功");
    }
}








